package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.converter.RecommendProductConverter;
import com.mall.shopping.dal.entitys.*;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 1530009312@qq.com
 * @date Created in 2021/6/18 22:50
 * @description
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    RecommendProductConverter recommendProductConverter;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductDetailConverter productDetailConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        try {
            // 有效参数校验
            request.requestCheck();

            Item item = itemMapper.selectByPrimaryKey(request.getId());
            String[] images = item.getImages();
            item.setImageBig(images[0]);
            // item.setImageSmall();

            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());

//            String[] images = item.getImages();
//            item.setImageBig(images[0]);
            ProductDetailDto productDetailDto = productDetailConverter.ProductToProductDto(item, itemDesc);
            productDetailDto.setProductImageSmall(images);

            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            productDetailResponse.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            productDetailResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();

        try {
            //有效参数校验
            request.requestCheck();

            //使用pageHelper进行分页查询
            PageHelper.startPage(request.getPage(), request.getSize());

            String sort = request.getSort();
            Integer priceGt = request.getPriceGt();
            Integer priceLte = request.getPriceLte();

            //从item表中进行查询，注意查询方式有
            //1.查询全部商品：默认无顺序：page、size有值、其余均为空值
            //2.查询所有商品：按照价格，从高到低    page、size、sort=1
            //3.查询所有商品：按照价格，从低到高    page、size、sort=-1
            //4.查询指定价格范围内的商品：priceGt（价格下限）、priceLte（价格上限）
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();

            List<Item> items;

            if ("1".equals(sort)) { //结果集按照价格升序排列
                items = itemMapper.selectItemByIncr(priceGt, priceLte);

            }else if ("-1".equals(sort)) {//结果集按照价格降序排列
                items = itemMapper.selectItemByDesc(priceGt, priceLte);

            }else{
                //结果集不排序
                items = itemMapper.selectItem(priceGt, priceLte);
            }

            //获得分页信息
            PageInfo<Item> itemPageInfo = new PageInfo<>(items);
            long total = itemPageInfo.getTotal();

            List<ProductDto> productDtos = productConverter.items2Dto(items);
            allProductResponse.setTotal(total);
            allProductResponse.setProductDtoList(productDtos);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            allProductResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            allProductResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();

        try{
            //首先直接从数据库的tb_panel表中查询 id = 6的记录即可
            Panel panel = panelMapper.selectByPrimaryKey(6);
            PanelDto panelDto = recommendProductConverter.PanelToPanelDto(panel);

            ArrayList<PanelDto> panelDtos = new ArrayList<>();
            //查询该板块下对应的商品内容
            Example example = new Example(PanelContent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("panelId",6);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

            for(PanelContent panelContent : panelContents) {
                Item item = itemMapper.selectByPrimaryKey(panelContent.getProductId());
                panelContent.setProductName(item.getTitle());
                panelContent.setSalePrice(item.getPrice());
                panelContent.setSubTitle(item.getSellPoint());
            }
            List<PanelContentItemDto> panelContentItemDtos = recommendProductConverter.PanelContentItemDtos(panelContents);

            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            recommendResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            recommendResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
        }

        return recommendResponse;
    }
}